#include<avr/io.h>
#include<avr/interrupt.h>
#include"encoder.h"

static encoder* current_encoder;
static encoder  default_encoder={1,0,100,-100,1};

void Init_encoder(void)
{
 EIMSK|=1<<INT0;
 EICRA=0x03;
 current_encoder=&default_encoder;
};

void set_encoder(encoder *p)
{
  current_encoder=p;
};


unsigned get_default_count(void)
{
 return default_encoder.count;
}

void clr_encoder(void)
{
  current_encoder=&default_encoder;
};


SIGNAL(INT0_vect)
{
  if ((PIND&(1<<PD1))==0) 
  {
   if (current_encoder->enable && current_encoder->count<current_encoder->top) 	current_encoder->count+=current_encoder->step; 
    
  } else
  {
   if (current_encoder->enable && current_encoder->count>current_encoder->bottom) current_encoder->count-=current_encoder->step;

  };
};
